package org.easybuy.daoimpol;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.easybuy.dao.BaseDao;
import org.easybuy.dao.UserDao;
import org.easybuy.entity.User;

/**
 * 
 * @用户表实现类
 * 
 * */
public class UserDaoimpol extends BaseDao implements UserDao {
	// 用户注册
	@Override
	
	public int insertUser(User user) {
		String sql = "insert into easybuy_user(loginName,`password`,`sex`,`email`,`mobile`)values(?,?,?,?,?)";
		Object[] parm = { user.getLoginName(), user.getPassword(),
				user.getSex(), user.getEmail(), user.getMoblie() };
		int result = this.executeUpdate(sql, parm);
		return result;
	}
	public int AllNum(int Number){
		String sql="select count(*) from easybuy_buycar";
		 Number=this.executeUpdate(sql);
		return Number;			
	}

	// 查询所有用,户真实姓名
	@Override
	public List<User> getAllUserName() {
		List<User> list = new ArrayList<User>();
		ResultSet rs = null;
		String sql = "SELECT userName FROM `easybuy_user`";
		try {
			rs = this.exxcuteQuery(sql);
			User user = null;
			while (rs.next()) {
				user = new User();
				user.setUaerName(rs.getString("userName").toString());
				list.add(user);
			}
		} catch (SQLException e) {
			BaseDao.closeAll(connection, pstmt, rs);
			e.printStackTrace();
		}
		return list;
	}

	// 查询所有邮箱
	@Override
	public List<User> getAllEmail() {
		List<User> list = new ArrayList<User>();
		ResultSet rs = null;
		String sql = "SELECT email FROM `easybuy_user`";
		try {
			rs = this.exxcuteQuery(sql);
			User user = null;
			while (rs.next()) {
				user = new User();
				user.setUaerName(rs.getString("email").toString());
				list.add(user);
			}
		} catch (SQLException e) {
			BaseDao.closeAll(connection, pstmt, rs);
			e.printStackTrace();
		}
		return list;
	}

	// 查询所有手机号
	@Override
	public List<User> getAllMoblie() {
		List<User> list = new ArrayList<User>();
		ResultSet rs = null;
		String sql = "SELECT moblie FROM `easybuy_user`";
		try {
			rs = this.exxcuteQuery(sql);
			User user = null;
			while (rs.next()) {
				user = new User();
				user.setUaerName(rs.getString("moblie").toString());
				list.add(user);
			}
		} catch (SQLException e) {
			BaseDao.closeAll(connection, pstmt, rs);
			e.printStackTrace();
		}
		return list;
	}

	// 查询所有用户姓名
	@Override
	public User getAllLoginName(String loginName) {
		ResultSet rs = null;
		User user = null;
		String sql = "SELECT * FROM `easybuy_user`where loginName = ?";
		try {
			rs = this.exxcuteQuery(sql, loginName);

			while (rs.next()) {
				user = new User();
				user.setLoginName(loginName);
			}
		} catch (SQLException e) {
			BaseDao.closeAll(connection, pstmt, rs);
			e.printStackTrace();
		}
		return user;
	}

	// 根据用户名查询密码
	@Override
	public User getUserPassword(String loginName, String password) {
		ResultSet rs = null;
		User user = null;
		String sql = "SELECT *FROM `easybuy_user` where loginName = ?and password=?";
		Object[] objects = { loginName, password };
		try {
			rs = this.exxcuteQuery(sql, objects);
			while (rs.next()) {
				user = new User();
				user.setPassword(String.valueOf("password"));
				user.setLoginName(String.valueOf("loginName"));
			}
		} catch (SQLException e) {
			BaseDao.closeAll(connection, pstmt, rs);
			e.printStackTrace();
		}
		return user;
	}

}
